package com.mjfeed.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.DeliveryCorp;
import com.mjfeed.service.DeliveryCorpService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 物流公司
 * @author zhaominglei
 * @date 2014年10月19日
 */
@Controller("adminDeliveryCorpController")
@RequestMapping("/admin/deliveryCorp")
public class DeliveryCorpController extends BaseController {
	@Resource(name = "deliveryCorpService")
	private DeliveryCorpService deliveryCorpService;
	
	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/deliveryCorp/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<DeliveryCorp> pagination = deliveryCorpService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/deliveryCorp/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/deliveryCorp/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(DeliveryCorp deliveryCorp, HttpServletRequest request, HttpServletResponse response, Model model){
		if (deliveryCorp == null) {
			return ERROR_VIEW;
		}
		deliveryCorpService.save(deliveryCorp);
		return "redirect:/admin/deliveryCorp/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("deliveryCorp", deliveryCorpService.getById(DeliveryCorp.class, id));
		return "admin/deliveryCorp/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(DeliveryCorp deliveryCorp, HttpServletRequest request, HttpServletResponse response, Model model){
		if (deliveryCorp == null || deliveryCorp.getId() == null || deliveryCorp.getId() < 1) {
			return ERROR_VIEW;
		}
		DeliveryCorp pDeliveryCorp = deliveryCorpService.getById(DeliveryCorp.class, deliveryCorp.getId());
		pDeliveryCorp.setName(deliveryCorp.getName());
		pDeliveryCorp.setUrl(deliveryCorp.getUrl());
		pDeliveryCorp.setCode(deliveryCorp.getCode());
		pDeliveryCorp.setOrders(deliveryCorp.getOrders());
		deliveryCorpService.update(pDeliveryCorp);
		return "redirect:/admin/deliveryCorp/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("deliveryCorp", deliveryCorpService.getById(DeliveryCorp.class, id));
		return "admin/deliveryCorp/view";
	}
}
